package bookdbws;

import accountws.SessionExpiredException;
import branchws.DBHandler;
import hqclient.BranchWebServiceWS_Service;
import java.util.ArrayList;
import java.util.List;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.xml.ws.WebServiceRef;

/**
 *
 * @author Atuan
 */
@WebService(serviceName = "BookdbWS")
public class BookdbWS {
    //@WebServiceRef(wsdlLocation = "WEB-INF/wsdl/localhost_8080/BookstoreChainHQWS/BranchWebServiceWS.wsdl")
    private BranchWebServiceWS_Service service;

    IBookDB dbh;

    public BookdbWS() {
        dbh = new DBHandler();
    }
        
    @WebMethod(operationName = "addbook")
    public Boolean addBook(Book book, String sid)
        throws SessionExpiredException {
        if(dbh.checkUser(sid)){            
            return dbh.addBook(book);
        } else {
            throw new SessionExpiredException();
        }
    }
    
    @WebMethod(operationName = "removeBook")
    public Boolean removeBook(String isbn, String sid)
        throws SessionExpiredException {
        if(dbh.checkUser(sid)){            
            return dbh.removeBook(isbn);
        } else {
            throw new SessionExpiredException();
        }
    }
    
    @WebMethod(operationName = "removecopyofbook")
    public Boolean removeCopyOfBook(String isbn, String sid)
        throws SessionExpiredException {
        if(dbh.checkUser(sid)){            
            return dbh.removeCopyOfBook(isbn);
        } else {
            throw new SessionExpiredException();
        }
    }
    
    @WebMethod(operationName = "getbook")
    public Book getBook(String isbn, String sid)
        throws SessionExpiredException {
        if(dbh.checkUser(sid)){            
            return dbh.getBook(isbn);
        } else {
            throw new SessionExpiredException();
        }
    }
    
    @WebMethod(operationName = "getallavailablebooks")
    public List<Book> getAllHQBooks(String sid)
        throws SessionExpiredException {   
        if(dbh.checkUser(sid)){
            List<hqclient.Book> b;
            try { // Call Web Service Operation
                service = new BranchWebServiceWS_Service();
                b = service.getBranchWebServiceWSPort().showBookCatalogue();
                System.out.println("getallavailablebooks");
                return mapToBookList(b);
            } catch (Exception ex) {
                System.out.println("error :( "+ex.getMessage());
                return null;
            }
        } else {
            throw new SessionExpiredException();
        }
    }
    
    @WebMethod(operationName = "searchbyTitle")
    public List<Book> searchByTitle(String title, String sid)
        throws SessionExpiredException {
        if(dbh.checkUser(sid)){            
            return dbh.searchForBooksByTitle(title);
        } else {
            throw new SessionExpiredException();
        }
    }
    
    @WebMethod(operationName = "searchbyauthor")
    public List<Book> searchByAuthor(String name, String surname, String sid)
        throws SessionExpiredException {
        if(dbh.checkUser(sid)){            
            return dbh.searchForBooksByAuthor(name, surname);
        } else {
            throw new SessionExpiredException();
        }
    }
    
    @WebMethod(operationName = "searchbyauthorSurname")
    public List<Book> searchByAuthorSurname(String surname, String sid)
        throws SessionExpiredException {
        if(dbh.checkUser(sid)){            
            return dbh.searchForBooksByAuthor(surname);
        } else {
            throw new SessionExpiredException();
        }
    }
    
    @WebMethod(operationName = "searchbypublisher")
    public List<Book> searchByPublisher(String name, String sid)
        throws SessionExpiredException {
        if(dbh.checkUser(sid)){            
            return dbh.searchForBooksByPublisher(name);
        } else {
            throw new SessionExpiredException();
        }
    }
    
    @WebMethod(operationName = "updateBook")
    public Boolean updateBook(Book book, String sid)
        throws SessionExpiredException {
        if(dbh.checkUser(sid)){            
            return dbh.updateBook(book);
        } else {
            throw new SessionExpiredException();
        }
    }
    
    @WebMethod(operationName = "getallbooks")
    public List<Book> getAllBranchBooks(String sid)
        throws SessionExpiredException {
        if(dbh.checkUser(sid)){            
            return dbh.getAllBooks();
        } else {
            throw new SessionExpiredException();
        }
    }
    
    @WebMethod(operationName = "getallauthors")
    public List<Author> getAllAuthors(String sid)
        throws SessionExpiredException {
        if(dbh.checkUser(sid)){            
            return null;//dbh.getAllBooks();
        } else {
            throw new SessionExpiredException();
        }
    }
    
    @WebMethod(operationName = "getallpublishers")
    public List<String> getAllPublishers(String sid)
        throws SessionExpiredException {
        if(dbh.checkUser(sid)){            
            return null;//dbh.getAllBooks();
        } else {
            throw new SessionExpiredException();
        }
    }
    
    
    private List<Author> mapToAuthorList(List<hqclient.Author> a){
        List<Author> auth = new ArrayList<Author>();
        for(int i = 0; i < a.size(); i++){
            auth.add(new Author(a.get(i).getId(), a.get(i).getName(),a.get(i).getSurname()));
        }        
        return auth;
    }
    
    private Publisher mapToPublisher(hqclient.Publisher p){
        return new Publisher(p.getId(), p.getName());
    }
    
    private List<Book> mapToBookList(List<hqclient.Book> b){
        List<Book> result = new ArrayList<Book>();
        for(int i= 0; i < b.size(); i++)
        {
            result.add(new Book(
                    b.get(i).getTitle(),
                    b.get(i).getIsbn(),
                    mapToPublisher(b.get(i).getPublisher()),
                    mapToAuthorList(b.get(i).getAuthors()), 
                    b.get(i).getAmount(),
                    b.get(i).getPrice()
                    ));
        }
        for(int i = 0; i < result.size(); i++){
            System.out.println("result"+i+" "+result.get(i).getIsbn()+" "+result.get(i).getTitle());
        }
        return result;
    }
    
    
    
    
//    @WebMethod(operationName = "test")
//    public void test(){
//        List<Book> books;
//        String sid = "552462048842027858311626admin";
//        try{
//            List<Author> a = new ArrayList<Author>();
////            a.add(new Author("Bret Easton","Ellis"));
////            addBook(new Book(
////                    "American Psycho",
////                    "978-83-89640-41-3",
////                    new Publisher("vis-a-vis/Etiuda"),
////                    a,4,9.99
////                    ),
////                    sid);
////            a.clear();
////            a = new ArrayList<Author>();
////            a.add(new Author("Phillip K.","Dick"));
////            addBook(new Book(
////                    "Ubik",
////                    "978-0-57507-921-2",
////                    new Publisher("Doubleday"),
////                    a,3,19.99
////                    ),
////                    sid);
////            books = getAllBranchBooks(sid);
////            for(int i = 0; i < books.size(); i++)
////            {
////                System.out.println("Book"+i+": "
////                        +books.get(i).getIsbn()+" "
////                        +books.get(i).getTitle()+" "
////                        +books.get(i).getPublisher().getName()+" "
////                        +books.get(i).getAmount()+" "
////                        +books.get(i).getNumberOfAuthors()+" "
////                        +books.get(i).getPrice());                
////            }
////            Book bookdiff = getBook("978-0-57507-921-2",sid);
////            if(bookdiff.getNumberOfAuthors()>0){
////                System.out.println("getBook(id): "+bookdiff.getTitle()
////                        +" "+bookdiff.getAuthor(0).getName()+" "
////                        +bookdiff.getAuthor(0).getSurname());
////            }
////            Book b1 = getBook("978-83-89640-41-3",sid);
////            if(b1.getNumberOfAuthors()>0){
////                System.out.println("getBook(id): "+b1.getTitle()
////                        +" "+b1.getAuthor(0).getName()+" "
////                        +b1.getAuthor(0).getSurname());
////            }
////            a.clear();
////            a = new ArrayList<Author>();
////            a.add(new Author("George R. R.","Martin"));
////            addBook(new Book(
////                    "Game Of Thrones",
////                    "0-553-10354-7",
////                    new Publisher("Bantam Spectra"),
////                    a,1,14.99
////                    ),
////                    sid);
////            a.clear();
////            a = new ArrayList<Author>();
////            a.add(new Author("Phillip K.","Dick"));
////            addBook(new Book(
////                    "Do Androids Dream of Electric Sheep?",
////                    "0-345-40447-5",
////                    new Publisher("Doubleday"),
////                    a,6,9.99
////                    ),
////                    sid);
////            a.clear();
////            a = new ArrayList<Author>();
////            a.add(new Author("Valerie","Martin"));
////            addBook(new Book(
////                    "Mary Reilly",
////                    "0-385-24968-3",
////                    new Publisher("Doubleday"),
////                    a,1,9.99
////                    ),
////                    sid);
////            System.out.println("before author search");
////            books = this.searchByAuthor("Phillip K.", "Dick", sid);
////            for(int i = 0; i < books.size(); i++)
////            {
////                System.out.println("Book"+i+": "
////                        +books.get(i).getIsbn()+" "
////                        +books.get(i).getTitle()+" "
////                        +books.get(i).getPublisher().getName()+" "
////                        +books.get(i).getAmount()+" "
////                        +books.get(i).getNumberOfAuthors()+" "
////                        +books.get(i).getPrice());                
////            }        
////            System.out.println("before publisher search");
////            books = this.searchByPublisher("Ace", sid);
////            for(int i = 0; i < books.size(); i++)
////            {
////                System.out.println("Book"+i+": "
////                        +books.get(i).getIsbn()+" "
////                        +books.get(i).getTitle()+" "
////                        +books.get(i).getPublisher().getName()+" "
////                        +books.get(i).getAmount()+" "
////                        +books.get(i).getNumberOfAuthors()+" "
////                        +books.get(i).getPrice());                
////            }       
////             
////            System.out.println("before 2nd publisher search");
////            books = this.searchByPublisher("Doubleday", sid);
////            for(int i = 0; i < books.size(); i++)
////            {
////                System.out.println("Book"+i+": "
////                        +books.get(i).getIsbn()+" "
////                        +books.get(i).getTitle()+" "
////                        +books.get(i).getPublisher().getName()+" "
////                        +books.get(i).getAmount()+" "
////                        +books.get(i).getNumberOfAuthors()+" "
////                        +books.get(i).getPrice());                
////            }       
////            System.out.println("before title search");
////            books = this.searchByTitle("Ubik", sid);
////            for(int i = 0; i < books.size(); i++)
////            {
////                System.out.println("Book"+i+": "
////                        +books.get(i).getIsbn()+" "
////                        +books.get(i).getTitle()+" "
////                        +books.get(i).getPublisher().getName()+" "
////                        +books.get(i).getAmount()+" "
////                        +books.get(i).getNumberOfAuthors()+" "
////                        +books.get(i).getPrice());                
////            }       
////            System.out.println("before surname search");
////            books = this.searchByAuthorSurname("Martin", sid);
////            for(int i = 0; i < books.size(); i++)
////            {
////                System.out.println("Book"+i+": "
////                        +books.get(i).getIsbn()+" "
////                        +books.get(i).getTitle()+" "
////                        +books.get(i).getPublisher().getName()+" "
////                        +books.get(i).getAmount()+" "
////                        +books.get(i).getNumberOfAuthors()+" "
////                        +books.get(i).getPrice());                
////            }         
////            a.clear();
////            a = new ArrayList<Author>();
////            a.add(new Author("test","test"));
////            addBook(new Book(
////                    "test title3",
////                    "test isbn2",
////                    new Publisher("test pub3"),
////                    a,2,9.99
////                    ),
////                    sid);
//            //removeCopyOfBook("test isbn",sid);
//            //removeBook("test isbn2",sid);
////            System.out.println("before test search for test book");
////            books = this.searchByAuthor("test","test", sid);
////            for(int i = 0; i < books.size(); i++)
////            {
////                System.out.println("Book"+i+": "
////                        +books.get(i).getIsbn()+" "
////                        +books.get(i).getTitle()+" "
////                        +books.get(i).getPublisher().getName()+" "
////                        +books.get(i).getAmount()+" "
////                        +books.get(i).getNumberOfAuthors()+" "
////                        +books.get(i).getPrice());                
////            }
////            a.clear();
////            a = new ArrayList<Author>();
////            a.add(new Author("test","test"));
////            updateBook(new Book(
////                    "test titleD",
////                    "test isbn2",
////                    new Publisher("test pub"),
////                    a,1,1.99
////                    ),
////                    sid);
//            removeBook("test isbn",sid);
//            System.out.println("after test update");                  
//        } catch(SessionExpiredException e){
//            System.err.println("error");
//        }
//    }    
}
